09. Deploying to Android
Deploying to Android
Android Studio Setup
To get started deploying your first VR app on to your Android device, go ahead and open Android Studio.
Note: You should already have downloaded and installed Android Studio and the Java SE Development Kit 8 (JDK) during the Before You Get Started > Installation lesson.
On the Welcome to Android Studio screen, click Configure > SDK Manager:

This will bring up the Android SDK settings where you can install/uninstall components of Android SDK and its related tools.
Note: To install/uninstall components, you check/uncheck the checkboxes next to them, and once you made the changes you want, click the Apply button.
In the SDK Platforms tab, we recommend installing the latest version that is not in beta:

Note: Google Cardboard requires Android 4.4 (KitKat) - API Level 19 or higher. You can install as many API levels as you like, however, one is enough as long as it is the same as, or higher than, the Android version on the device you are deploying to.
Now, switch over to the SDK Tools tab. Here you should install Android SDK Build-Tools, Android SDK Platform-Tools, Android SDK Tools, and if you are using Windows, Google USB Driver:

Note: Some newer Android phones don't require Google USB Driver, so you might not need to install this component.
Important! Because Unity, Google VR SDK, Android SDK, and JDK are in continuous development, not all combinations of these versions will work together. The combination of versions shown in the screenshot above has been tested and works well with the Unity and Google VR SDK versions recommend for this course.
Next, make a note of the Android SDK Location in the path field:

This is the location where the Android SDK will be installed and you will need the path to this location when you set up Unity's External Tools for Android below.
Note: You can install Android SDK where you like. Default locations are:
- Windows:
C:\Users<YourUserName>\AppData\Local\Android\Sdk- macOS:
/Users/<YourUserName>/Library/Android/Sdk
And with that, you can click the Apply button to begin installing/uninstalling the Android SDK components according to your selection.
Android Phone Setup
While the Android SDK is installing, let’s set up your phone so it can be used for development. To do that, you will need to navigate to Settings > About Phone > Build Number.
Note: Depending on your phone, the location where the Build Number is displayed may vary slightly, but it should always be in the About Phone section of your phone's Settings.
Once you find the Build number, tap on it 7 or more times. This will unlock Developer Mode.
With Developer Mode enabled, you can now navigate to Settings > Developer Options and enable USB Debugging. This will allow you to deploy apps to your phone via USB.
Unity Setup
External Tools for Android
Once that is all set up, find a USB charging cable and connect your phone to your computer.
With your Udacity Carnival project open, navigate to the Unity menu Edit > Preferences… if you are using Windows or Unity > Preferences… if you are using macOS.
This will bring up the Unity Preferences pop-up window. Here, select External Tools and scroll down to the Android section:

Fill out the SDK and JDK fields by using the Browse buttons to navigate to where you installed the Android SDK previously in this lesson and the Java SE Development Kit 8 (JDK) in the Before You Get Started > Installation lesson.
Note: The exact paths to the SDK and JDK on your computer depends on where you install them. Default locations are:
Windows
- SDK:
C:/Users/<YourUserName>/AppData/Local/Android/sdk- JDK:
C:/Program Files/Java/jdk1.8.0_171macOS
- SDK:
/Users/<YourUserName>/Library/Android/sdk- JDK:
/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
Help! I forgot the locations of the Android SDK or JDK? Where can I look?
If you forgot the location of your Android SDK, you can look it up. Open up Android Studio to get to the welcome screen. On the bottom, click on Configure > SDK Manager. This should open up the Default Settings. Under Appearance & Behavior > System Settings > Android SDK (on the right), you should be able to see the Android SDK location at the top.
In Windows 10, you can look up your Java JDK location by opening the "Configure Java" app. Go into the Java tab and clicking on the "View…" button to opne up the Java Runtime Environment Settings. Look for the "Path" column. When you copy this location to add it into Unity, you'll remove the "bin" folder and anything after it. In this example image, it would be just "C:\Program Files\Java\jre1.8.0.171".

Tip: If you're getting build errors that refer to the Android SDK or JDK (read about these in the Console window of Unity), be sure to confirm that you have these setup properly. This helps resolve most issues.
Important! Depending on your OS system settings, some of the files/folders in these locations might be hidden. If that's the case for you, you need to enable the "Show hidden files and folders" option to be able to see and navigate to them. To do this, you can follow these steps:
Windows
- Open File Explorer.
- Select View > Options > *Change folder and search options.
- Select the View tab and, in Advanced settings, select Show hidden files, folders, and drives and OK.
Alternatively, you can toggle this setting on/off by using the Hidden Items checkbox in the File Explorer Ribbon.
macOS
- Open Terminal
- Run the following commands, i.e. copy/paste them into Terminal and press the return key:
defaults write com.apple.Finder AppleShowAllFiles true
killall FinderIf you want to change it back, follow the same steps, but replace "true" with "false" on the first line.
With that information provided, the general setup required to deploy a Unity project to your Android device is complete.
Build Settings
In the Unity menu, go to File > Build Settings… to open up the Builds Settings pop-up window:

In the Platform list, select Android, then the Switch Platform button. This process may take a few minutes because Unity will need to reimport all the assets and prepare them for the Android platform.
Player Settings
The last step to check is the Player Settings. You can access these settings in the Unity menu Edit > Project Settings > Player or by clicking the Player Settings… button in the Builds Settings pop-up window. This will bring up a lot of settings in the Inspector:

In this starter project, we have prepared the settings for you. However, because this now is your project, you will want to modify the Company Name and Bundle Identifier with your own information:
- Company Name:
Firstname Lastname - Package Name:
com.firstnamelastname.udacitycarnival
Note: Leaving the pre-filled Package Name,
com.udacity.udacitycarnival, unchanged might prevent you from deploying the project to your Android device.
It's also a good idea to double-check that Cardboard support is enabled in the XR Settings:

Now, the project is ready to be deployed to your Android device.
Build and Run
In the Builds Settings popup window, click the Build and Run button:

When you click the Build and Run button, Unity will prompt you for a location to save the .apk file. Let’s create a new empty folder named Builds. We recommend placing this folder at the repo level, i.e., the parent folder of your Unity project.
Note: It's also common to place the Builds folder in the Unity project folder, i.e., at the same level as the Assets folder.
Give the .apk file a descriptive name and click the Save button:

Note: Never place your builds inside any of the Unity generated folders such as Assets, Library, ProjectSettings, etc., as this can break your project.
Unity will now create the build, i.e. the .apk file, and then install and launch it on your Android device.
Tip: If you want to install the app on another Android device, you can simply access the .apk file you just created from that Android device, for example, via Google Drive, and follow the on-screen prompts to install it.